API Tutorial I
What is an API???
API-stands for Application Programming Interface. If you look at the Windows System directory, typically \Windows\System under Window 95/98 and \Winnt\System32 under Windows NT, you will find a number of Dynamic Link Library (.DLL) files. These files contain functions that are used to run the windows os(operating system). These files make up the Windows API. The Windows API is a collection of routines available to the programmer. In a way, these API routines work just like Visual Basic's own internal functions. When you need to use the code in an API routine, your Visual Basic program calls that routine. When the Windows API finishes, control returns to your program so that it can continue.
Why do u need to use API???
Ever wondered how those programs got the name of your computer??,how those programs add an icon to the system tray??,how programs start as soon as windows starts??.Let's accept it ,Visual Basic has severe limitations in itself. You cannot perform the above tasks just by using plain VB.Here is where the API comes into play. So many Windows API routines exist that just about anything you can do from Windows, you can do from a Visual Basic application by calling the appropriate Windows API routine. In short "Anything any application can do ,your application can do too". You can even force a system reboot by calling the appropriate Windows API routine.
Great, Now how do I use the API???
Now Before you can use API in you application you need to define the appropriate API. For e.g. you want to force a windows logoff ,you must use the Exit Window API .The declaration for the ExitWindow API is as follows
Declare Function ExitWindows Lib "user32" Alias "ExitWindows" (ByVal dwReserved As Long, ByVal uReturnCode As Long) As Long
The function ExitWindows is defined in the user32.dll file. It requires 2 parameters ,dwReserved and uReurnCode.These 2 parameters should always be 0.
After u have declared this api function u need to call it .This is how u do it. Let's say u want to call the function when a user clicks a command button.
dim lreturn as long
Private Sub Command1_Click()
lreturn=ExitWindows(0,0)
End Sub
lreturn is the return value you get upon calling ExitWindows.If the function is successful lreturn will have a non zero value. If it fails then lreturn will have a zero value.
How can I remember all the API functions and their declarations???
You don't have to remember their declaration. Visual Studio comes with a tool called the API text viewer. You can find it in /Programs/Microsoft Visual Studio/Microsoft Visual Studio tools/API Text Viewer. All u have to do is load the win32api.txt file and voila u have all the declarations at your finger tips and all u have to do is the famous copy/paste routine.
What is a Handle???
A variable that identifies an object; an indirect reference to an operating system resource. In plain English a handle is variable of type long which uniquely identifies any object like forms,desktop,menus or in other words a handle is a unique id for each of these objects. Every window in the Windows operating system is identified by a handle. The desktop window has a handle, a Visual Basic form displayed in an application has a handle, and even the controls on a form, which are themselves actually windows, have handles. You can gather a lot of information about the windows in your application after you get the handle of the window that interests you.
Comments,Suggestions,Questions send me a mail at venky@venkydude.com .Do Visit my Code projects page for some cool VB codes.